<?php

namespace Block\Controllers;

use Illuminate\Http\Request;
use Block\Model\CoinList;
use Block\Model\Language;

/**
 *  币库首页控制器
 */
class IndexController extends Controller
{
    
    protected $lang;
    /**
     * 构造函数
     */
    public function __construct()
    {
        //获取已开启的语言
        $this->lang = Language::where('status', '0')->get();
    }

    /**
     * 仪表盘
     *
     * @return void
     */
    public function index(){

        return response()->json(['name' => 'Block', 'state' => 'ok']);
       
    }

    /**
     * 币种列表
     *
     * @return void
     */
    public function coin(){
        $Coin=new CoinList();
        $data=$Coin->orderBy('rank','ASC')->paginate(15);
        $response=$data->toArray();
        foreach ($response['data'] as $key => $value) {
            
            unset($response['data'][$key]['key_id']);
            unset($response['data'][$key]['created_at']);
            unset($response['data'][$key]['updated_at']);
            unset($response['data'][$key]['deleted_at']);
            $response['data'][$key]['icon']="https://blockchains.oss-cn-shanghai.aliyuncs.com/static/coinInfo/".$value['key_id'].".png";
        }
        return $this->response($response);
      
    }

    

    /**
     * 币种详情
     *
     * @return void
     */
    public function details(string $id){
        $Coin=new CoinList();
        $data=$Coin->find($id,["key_id","name","symbol","rank","price_usd","price_btc","24h_volume_usd","market_cap_usd","available_supply","total_supply","max_supply","percent_change_1h","percent_change_24h","percent_change_7d","last_updated"]);
        $lang=$data->lang->toArray();
        $extend=$data->extend->toArray();
        $response=[];
        if($data){
            $response=$data->toArray();
            $langIndex=array_search('zh',array_column($lang,'lang'));
            $response['name']=$lang[$langIndex]['name'];
            // $response['extend']= array_only($extend,['type','url']);
            $response=array_merge($response,['description'=>$lang[$langIndex]['description']]);
            unset($response['lang']);
           return $this->response($response);
            
        }else{
            return $this->response([],404,'Not found');
        }
        
    }
}
